@extends('layouts.header')
@section('style')
    hr {
        margin-top: 15px;
    }
@endsection
@section('content')
<article class="page-container">
    <div class="col-xs-offset-2">
        <span class="btn btn-primary radius" id="syncPos">同步位置</span>
    </div>
    <form class="form form-horizontal" id="form" autocomplete="off" enctype="multipart/form-data">
        <div class="row cl">
            <label class="form-label col-sm-2">报告模板：</label>
            <label class="form-label col-sm-4" style="text-align: left;">
                <select class="input-text" id="changeTemp">
                    @foreach($data_tmp as $tmp)
                        <option @if($tmp->id === $data->id) selected @endif value="{{$tmp->id}}">{{$tmp->name}}</option>
                    @endforeach
                </select>
            </label>
        </div>
        <div class="row cl">
            <label class="form-label col-sm-2">样品类型：</label>
            <label class="form-label col-sm-4" style="text-align: left;"> {{ $info_data->name }}</label>
            <label class="form-label col-sm-2">检测标准：</label>
            <label class="form-label col-sm-4" style="text-align: left;"> {{ $info_data->num }}</label>
        </div>
        <input type="hidden" name="std_id" value="{{ $info_data->std_id }}">
        <input type="hidden" name="tool_name" value="{{ $info_data->name }}">
        <input type="hidden" name="pos_id" value="{{ $data->id }}">
        <hr>

        <div class="row cl">
            <label class="form-label col-sm-2">检测单位：</label>
            <div class="formControls col-sm-4">
                <input type="text" class="input-text" value="{{ $data->company }}" placeholder="" name="company">
            </div>
            <label class="form-label col-sm-2">报告编号：</label>
            <div class="formControls col-sm-4">
                <input type="text" class="input-text" value="{{ $data->report_num }}" placeholder="" name="report_num">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-sm-2">到样日期：</label>
            <div class="formControls col-sm-4">
                <input type="text" class="input-text" value="{{ $data->come_date }}" placeholder="" name="come_date">
            </div>
            <label class="form-label col-sm-2">检验日期：</label>
            <div class="formControls col-sm-4">
                <input type="text" class="input-text" value="{{ $data->test_date }}" placeholder="" name="test_date">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-sm-2">样品数量：</label>
            <div class="formControls col-sm-4">
                <input type="text" class="input-text" value="{{ $data->sample_count }}" placeholder=""
                       name="sample_count">
            </div>
            <label class="form-label col-sm-2">环境温度：</label>
            <div class="formControls col-sm-4">
                <input type="text" class="input-text" value="{{ $data->temp }}" placeholder="" name="temp">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-sm-2">相对湿度：</label>
            <div class="formControls col-sm-4">
                <input type="text" class="input-text" value="{{ $data->hum }}" placeholder="" name="hum">
            </div>
            <label class="form-label col-sm-2">大气压力：</label>
            <div class="formControls col-sm-4">
                <input class="input-text" type="text" value="{{ $data->pressure }}" placeholder="" name="pressure"  />
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-sm-2">天气：</label>
            <div class="formControls col-sm-4">
                <input type="text" class="input-text" value="{{ $data->wea }}" placeholder="" name="wea">
            </div>
            <label class="form-label col-sm-2">检测地点：</label>
            <div class="formControls col-sm-4">
                <input class="input-text" type="text" value="{{ $data->test_address }}" placeholder="" name="test_address"  />
            </div>
        </div>
        <hr>
        <div class="row cl">
            <label class="form-label col-sm-2">开始行号：</label>
            <div class="formControls col-sm-4">
                <input type="text" class="input-text" value="{{ $data->line_num }}" placeholder="必填" name="line_num">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-sm-2">单样品试验数据占行数：</label>
            <div class="formControls col-sm-4">
                <input type="text" class="input-text" value="{{ $data->sample_rowspann }}" placeholder="必填" name="sample_rowspann">
            </div>
        </div>
        <div class="row cl c-red text-c"> 以下只需写出列号</div>
        <div class="row cl">
            <label class="form-label col-sm-2">序号开始：</label>
            <div class="formControls col-sm-4">
                <input type="text" class="input-text" value="{{ $data->serial_num1 }}" placeholder=""
                       name="serial_num1">
            </div>
            <label class="form-label col-sm-2">序号结束：</label>
            <div class="formControls col-sm-4">
                <input type="text" class="input-text" value="{{ $data->serial_num2 }}" placeholder=""
                       name="serial_num2">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-sm-2">样品号开始：</label>
            <div class="formControls col-sm-4">
                <input type="text" class="input-text" value="{{ $data->sample_num1 }}" placeholder=""
                       name="sample_num1">
            </div>
            <label class="form-label col-sm-2">样品号结束：</label>
            <div class="formControls col-sm-4">
                <input type="text" class="input-text" value="{{ $data->sample_num2 }}" placeholder=""
                       name="sample_num2">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-sm-2">样品自编号开始：</label>
            <div class="formControls col-sm-4">
                <input type="text" class="input-text" value="{{ $data->self_num1 }}" placeholder="" name="self_num1">
            </div>
            <label class="form-label col-sm-2">样品自编号结束：</label>
            <div class="formControls col-sm-4">
                <input type="text" class="input-text" value="{{ $data->self_num2 }}" placeholder="" name="self_num2">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-sm-2">额定电压开始：</label>
            <div class="formControls col-sm-4">
                <input type="text" class="input-text" value="{{ $data->voltage1 }}" placeholder="" name="voltage1">
            </div>
            <label class="form-label col-sm-2">额定电压结束：</label>
            <div class="formControls col-sm-4">
                <input type="text" class="input-text" value="{{ $data->voltage2 }}" placeholder="" name="voltage2">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-sm-2">分类开始：</label>
            <div class="formControls col-sm-4">
                <input type="text" class="input-text" value="{{ $data->class1 }}" placeholder="" name="class1">
            </div>
            <label class="form-label col-sm-2">分类结束：</label>
            <div class="formControls col-sm-4">
                <input type="text" class="input-text" value="{{ $data->class2 }}" placeholder="" name="class2">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-sm-2">判定开始：</label>
            <div class="formControls col-sm-4">
                <input type="text" class="input-text" value="{{ $data->res1 }}" placeholder="" name="res1">
            </div>
            <label class="form-label col-sm-2">判定结束：</label>
            <div class="formControls col-sm-4">
                <input type="text" class="input-text" value="{{ $data->res2 }}" placeholder="" name="res2">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-sm-2">有效日期开始：</label>
            <div class="formControls col-sm-4">
                <input type="text" class="input-text" value="{{ $data->valid_date1 }}" placeholder=""
                       name="valid_date1">
            </div>
            <label class="form-label col-sm-2">有效日期结束：</label>
            <div class="formControls col-sm-4">
                <input type="text" class="input-text" value="{{ $data->valid_date2 }}" placeholder=""
                       name="valid_date2">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-sm-2">默认报告模板：</label>
            <label class="form-label col-sm-4" style="text-align: left;">
                <div class="skin-minimal">
                    <div class="radio-box">
                        <label><input type="radio" value="0" name="is_default"
                                      @if ($data->is_default==0) checked @endif >否</label>
                    </div>
                    <div class="radio-box">
                        <label><input type="radio" value="1" name="is_default"
                                      @if ($data->is_default==1) checked @endif>是</label>
                    </div>
                </div>
            </label>
            <label class="form-label col-sm-2">报告模板名称：</label>
            <div class="formControls col-sm-4">
                <input type="text" class="input-text" value="{{ $data->name}}" placeholder="" name="name">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-sm-2">模板文件：</label>
            <div class="formControls col-sm-10">
                    <span class="btn-upload form-group">
                        <input class="input-text upload-url radius" type="text" readonly width="245px"
                               placeholder="必须上传Word，后缀为“.docx”">
                        <a href="javascript:void();" class="btn btn-primary radius">浏览</a>
                        <input type="file" name="file" class="input-file" accept="application/vnd.openxmlformats-officedocument.wordprocessingml.document">
                    </span>
                @if ($exist_template)
                    <a data-href="{{substr($template_file,1)}}" class="btn btn-primary radius r" id="downtemp"> <i class="Hui-iconfont Hui-iconfont-down"></i> 下载模板</a>
                @endif
            </div>
        </div>

        @csrf

        <div class="row cl pt-20 text-c">
            <input class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
            <input class="btn btn-primary radius ml-50"  onclick="add()" value="&nbsp;&nbsp;添加&nbsp;&nbsp;">
        </div>
    </form>
</article>

<script type="text/javascript">
    $(function () {

        var index2;

        $('.skin-minimal input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        });

        $("#form").validate({
            rules: {
                line_num: {
                    required: true,
                    isPoInteger: true,
                },
            },
            // onkeyup:false,
            // focusCleanup:false,
            success: "valid",
            submitHandler: function (form) {
                $(form).ajaxSubmit({
                    type: "post",
                    url: "",//自己提交给自己可以不写url
                    //beforeSend  beforeSubmit

                    beforeSend: function () {
                        //0.4透明度 白色遮罩
                        index2 = layer.load(2, {shade: [0.3, '#fff']});
                    },
                    complete: function () {
                        layer.close(index2);
                    },
                    success: function (data) {
                        //判断添加结果
                        if (data === '1') {
                            window.location.reload();//刷新
                            parent.layer.msg('操作成功!', {icon: 1, time: 1000});
                            // layer_close();//关闭自己
                        } else {
                            let msg = data !== '0' ? data : '操作失败!';
                            layer.msg(msg, {icon: 2, time: 2000});
                        }
                    },
                    error: function (data) {
                        if (data.status == 422) {
                            // var json=JSON.parse(data.responseText);
                            var json = $.parseJSON(data.responseText);
                            allError = '';
                            $.each(json.errors, function (index, el) {
                                allError += el + '<br>';
                            });
                            //输出错误信息
                            layer.open({
                                content: allError,
                                title: false,
                                type: 0,
                                icon: 5,
                                // skin: 'layui-layer-molv',
                                btn: '',
                                closeBtn: 2, //右上角关闭按钮风格  0无,1默认,2
                                shadeClose: true, //开启遮罩关闭
                                time: 0,
                                anim: 6,//弹出动画
                            });
                        } else {
                            ajax_error(data);
                        }
                    }
                });
            }
        });
    });

    $('#downtemp').on('click',function(){
        window.open($(this).data('href')+'?id='+new Date(),'_blank');
    })
    $('#changeTemp').on('change', function () {
        let oData = {}, param = {}, url = '', type = 'get';
        param.pos_id = $(this).val();
        ajax(type, url, param, callback,0);

        function callback(res) {
            let inputName = '';
            $('input[type=text]', '#form').each(function () {
                inputName = $(this).attr('name');
                $(this).val(res.data[inputName]);
            })
            $('input[name=is_default]').each(function () {
                if (parseInt($(this).val()) === parseInt(res.data.is_default)) {
                    $(this).parents('.iradio-blue').trigger('click');
                }
            })
            $('input[name=pos_id]').val(res.data.id);
            if (res.exist_template) {
                $('#downtemp').removeClass('disabled');
                $('#downtemp').attr('href', res.template_file.substring(1) + '?id=' +{{rand(1,999)}});
            } else {
                $('#downtemp').addClass('disabled');
            }
        }
    })

    $('#syncPos').on('click',function () {
        let content = '<form class="form form-horizontal" id="form" autocomplete="off" enctype="multipart/form-data">\n' +
            '        <div class="row cl">\n' +
            '            <label class="form-label col-sm-2">选择标准：</label>\n' +
            '            <label class="form-label col-sm-4" style="text-align: left;">\n' +
            '                <select class="input-text" id="toStd">\n' +
            '                    @foreach($o_std_rels as $item)' +
            '                        <option value="{{$item->id}}">{{$item->num}}</option>\n' +
            '                    @endforeach' +
            '                </select>\n' +
            '            </label>\n' +
            '        </div>' +
            '</form>';
        layer.open({
            type: 1,
            offset: 'auto',
            area: ['400px', '300px'],
            title: "", //不显示标题
            content: content, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
            btn: ['提交', '取消'],
            yes: function (index) {
                let from_id = $('#toStd').val(),to_id='{{ $info_data->id }}';
                ajax('post', 'syncPos', {from_id:from_id,to_id:to_id}, syncPosBak, 1);
            },
            cancel: function () {
                //右上角关闭回调
            }
        });
    })
    function syncPosBak(res) {
        if(res == 1){
            location.reload();
            return;
        }
        layer.msg('同步出错！数据未改变！');
    }
    function add() {
        var formData = new FormData($("#form")[0]);
        formData.append('std_tool_rel_id',{{request('id')}});
        ajax('post','add',formData,addBak,1,1,false,false);
        function addBak(data){
            //判断添加结果
            if (data === '1') {
                window.location.reload();//刷新
                parent.layer.msg('操作成功!', {icon: 1, time: 1000});
                // layer_close();//关闭自己
            } else {
                let msg = data !== '0' ? data : '操作失败!';
                layer.msg(msg, {icon: 2, time: 2000});
            }
        }
    }
</script>
@endsection
